package com.zsc.edu.gateway.modules.iot.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.edu.gateway.framework.mybatisplus.DataPermission;
import com.zsc.edu.gateway.modules.iot.product.dto.ProductDto;
import com.zsc.edu.gateway.modules.iot.product.entity.Product;
import com.zsc.edu.gateway.modules.iot.product.query.ProductQuery;
import com.zsc.edu.gateway.modules.iot.product.service.ProductService;
import com.zsc.edu.gateway.modules.operationLog.entity.OperationLogAnnotation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhuang
 */
@AllArgsConstructor
@RestController
@RequestMapping("api/rest/product")
public class ProductController {

    private final ProductService service;


    /**
     * 创建产品
     *
     * @param dto 创建的产品
     * @return 新的产品
     */
    @PostMapping
    @PreAuthorize("hasAuthority('iot:product:create')")
    @OperationLogAnnotation(content = "'产品'", operationType = "新建")
    public Product create(@RequestBody ProductDto dto) {
        return service.create(dto);
    }


    /**
     * 更新产品
     *
     * @return 更新后的产品
     */
    @PatchMapping("{id}")
    @PreAuthorize("hasAuthority('iot:product:update')")
    @OperationLogAnnotation(content = "'产品'", operationType = "更新")
    public Product update(@RequestBody ProductDto dto, @PathVariable("id") Long id) {
        return service.update(dto, id);
    }


    /**
     * 查询产品
     *
     * @param query    产品的查询表单
     * @param page  分页参数
     * @return Page<Device> 产品分页数据
     */
    @DataPermission
    @GetMapping
    @PreAuthorize("hasAuthority('iot:product:query')")
    public Page<Product> page(Page<Product> page, ProductQuery query) {
        return service.page(page, query.wrapper());
    }

    /**
     * 模糊查询名字，最多返回5条数据
     *
     * @param query 查询参数
     * @return 部门列表
     */
    @GetMapping("fuzzy")
    @PreAuthorize("hasAuthority('iot:product:query')")
    public List<Product> list(ProductQuery query) {
        return service.list(query);
    }

    /**
     * 删除产品
     *
     * @param id 产品编号
     * @return 被删除的产品
     */
    @DeleteMapping("{id}")
    @PreAuthorize("hasAuthority('iot:product:delete')")
    @OperationLogAnnotation(content = "'产品'", operationType = "删除")
    public Boolean delete(@PathVariable("id") Long id) {
        return service.delete(id);
    }

    /**
     * 产品
     * 查询详情
     *
     * @param id ID
     * @return 任务
     */
    @GetMapping("{id}")
    @PreAuthorize("hasAuthority('iot:product:query')")
    public Product detail(@PathVariable("id") Long id) {
        return service.detail(id);
    }

    /**
     * 查询产品各类信息
     */
    @GetMapping(value = "/ws/product/status")
    @DataPermission
    @PreAuthorize("hasAuthority('iot:product:query')")
    public Object status() {
        return service.status();
    }


    /**
     * 删除图片或预览图
     */
    @DeleteMapping("/attachment/{id}")
    @OperationLogAnnotation(content = "'设备图片或预览图'", operationType = "删除")
    public Boolean deleteIconOrPreview(@PathVariable("id") Long id, String attachmentId) {
        return service.deleteIconOrPreview(id, attachmentId);
    }
}

